﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace csglxt.DAL
{
    class DBHelper
    {
        private const string zfc =@"Data Source=.\SQLEXPRESS;Initial Catalog=useinfoDB;Integrated Security=True";
        private SqlConnection coon = new SqlConnection(zfc);
        public void Open()
        {
            coon.Open();
        }
        public void Close() 
        {
            coon.Close();
        }
        /// <summary>
        /// 执行非查询语句并获取影响行数
        /// </summary>
        /// <param name="sql"></param>
        /// <returns>cont</returns>
        public int ExecuteNonQuery(string sql) 
        {
            Open();
            int cont = new SqlCommand(sql, coon).ExecuteNonQuery();
            Close();
            return cont;
        }
        /// <summary>
        /// 查询一个值
        /// </summary>
        /// <param name="sql"></param>
        /// <returns>cont</returns>
        public object GetOne(string sql)
        {
            Open();
            object cont = new SqlCommand(sql, coon).ExecuteScalar();
            Close();
            return cont;
        }
        /// <summary>
        /// 查询并获取DataReader,请使用完后记得关闭连接
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public SqlDataReader GetAll(string sql)
        {
            Open();
            return new SqlCommand(sql, coon).ExecuteReader();
        
        }
        /// <summary>
        /// 获取表
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public DataTable GetTable(string sql)
        {
            DataTable dt = new DataTable();
            SqlDataAdapter dr = new SqlDataAdapter(sql, coon);
            dr.Fill(dt);
            return dt;
        }
    }
}
